﻿Public Class 设置
    Private Dr As New Draw
    Private T As New Form2.JSetting
    Private SF As New StringFormat
    Private _Old As Bitmap
    Private _Old2 As Bitmap
    Private Sub LoadFonts()
        Try
            Dim FM As New System.Drawing.Text.InstalledFontCollection
            For Each f As FontFamily In FM.Families
                FontList.Add(f.Name)
                Application.DoEvents()
            Next
            AddFontList()
        Catch
        End Try
    End Sub
    Private Sub AddFontList()
        For Each S As String In FontList
            ComboBox1.Items.Add(S)
        Next
        ComboBox1.Enabled = True
        Label3.Hide()
        ComboBox1.SelectedItem = Form2._T._sFont.Name
    End Sub

    Private Sub 设置_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SF.LineAlignment = StringAlignment.Center
        SF.Alignment = StringAlignment.Far
        Label8.Text = My.Resources.BB.Replace("%BB%", Application.ProductVersion).Replace("%T%", Form2.SL.Count & "条")
    End Sub
    Private Sub LoadSetting()
        If Me.Created Then
            With T
                ._LgColor1 = Me.Button1.BackColor
                ._LgColor2 = Me.Button2.BackColor
                ._sFont = New Font(ComboBox1.Text, Me.NumericUpDown2.Value, FontStyle.Regular, GraphicsUnit.Point)
                .IsOutLine = CheckBox1.Checked
                .IsShadow = CheckBox2.Checked
                .TimeInt = Me.NumericUpDown1.Value
                ComboBox1.Text = ._sFont.Name
            End With
            Dim S As String = "名言警句名言警句名言警句"
            If Label1.Visible = True Then
                If _Old IsNot Nothing Then _Old.Dispose()
                _Old = New Bitmap(Label1.Width, Label1.Height)
                Using G As Graphics = Graphics.FromImage(_Old)
                    G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
                    Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, T._sFont.Height), T._LgColor1, T._LgColor2)
                        If T.IsOutLine = True Then
                            Dr.DrawTextOutLine(Color.Black, G, S, SF, T._sFont, New Size(Label1.Width, Label1.Height))
                        End If
                        G.DrawString(S, T._sFont, Lg, New Rectangle(0, 0, Label1.Width, Label1.Height), SF)
                    End Using
                    If _Old2 IsNot Nothing Then _Old2.Dispose()
                    If T.IsShadow = True Then
                        _Old2 = New Bitmap(Dr.DrawShadow(_Old))
                    Else
                        _Old2 = New Bitmap(_Old)
                    End If
                End Using
                Label1.BackgroundImage = _Old2
            End If
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        LoadSetting()
    End Sub

    Private Sub NumericUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown2.ValueChanged
        LoadSetting()
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If Me.Created Then LoadSetting()
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If Me.Created Then LoadSetting()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Cdg As New ColorDialog
        If Cdg.ShowDialog <> DialogResult.Cancel Then
            Button1.BackColor = Cdg.Color
            LoadSetting()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Cdg As New ColorDialog
        If Cdg.ShowDialog <> DialogResult.Cancel Then
            Button2.BackColor = Cdg.Color
            LoadSetting()
        End If
    End Sub

    Private Sub 设置_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        If Me.Created Then
            CheckReg()
            RegIsOK.Hide()
            If FontList.Count = 0 Then
                ComboBox1.Enabled = False
                Label3.Show()
                LoadFonts()
            Else
                AddFontList()
            End If
            LoadSetting()
            With T
                ._LgColor1 = Form2._T._LgColor1
                ._LgColor2 = Form2._T._LgColor2
                ._sFont = Form2._T._sFont
                .IsOutLine = Form2._T.IsOutLine
                .IsShadow = Form2._T.IsShadow
                .TimeInt = Form2._T.TimeInt
                ComboBox1.Text = ._sFont.Name
                Me.Button1.BackColor = ._LgColor1
                Me.Button2.BackColor = ._LgColor2
                CheckBox1.Checked = .IsOutLine
                CheckBox2.Checked = .IsShadow
                Me.NumericUpDown2.Value = ._sFont.Size
                Me.NumericUpDown1.Value = .TimeInt
            End With
            Try
                Dim f As String = Application.StartupPath
                f = IIf(f.EndsWith("\"), f, f & "\")
                If IO.File.Exists(f & "名言警句语句库.txt") = True Then
                    TextBox1.Text = IO.File.ReadAllText(f & "名言警句语句库.txt", System.Text.Encoding.Default)
                End If
            Catch
            End Try
        End If
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.Checked = True Then
            Dim SrtPth As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Startup)
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form2._T = T
        With T
            ._LgColor1 = Me.Button1.BackColor
            ._LgColor2 = Me.Button2.BackColor
            ._sFont = New Font(ComboBox1.Text, NumericUpDown2.Value, FontStyle.Regular, GraphicsUnit.Point)
            .IsOutLine = CheckBox1.Checked
            .IsShadow = CheckBox2.Checked
            .TimeInt = NumericUpDown1.Value
        End With
        Try
            SaveSetting("DS名言警句", "Setting", "_LgColor1", Me.Button1.BackColor.R & "," & Me.Button1.BackColor.G & "," & Me.Button1.BackColor.B)
            SaveSetting("DS名言警句", "Setting", "_LgColor2", Me.Button2.BackColor.R & "," & Me.Button2.BackColor.G & "," & Me.Button2.BackColor.B)
            SaveSetting("DS名言警句", "Setting", "_sFont", ComboBox1.Text)
            SaveSetting("DS名言警句", "Setting", "IsOutLine", CheckBox1.Checked)
            SaveSetting("DS名言警句", "Setting", "IsShadow", CheckBox2.Checked)
            SaveSetting("DS名言警句", "Setting", "ftSize", NumericUpDown2.Value)
            SaveSetting("DS名言警句", "Setting", "TimeInt", NumericUpDown1.Value)
            SaveSetting("DS名言警句", "Setting", "CustomLines", RadioButton2.Checked)
        Catch
        End Try
        Try
            If TextBox1.TextLength <> 0 Then
                Dim f As String = Application.StartupPath
                f = IIf(f.EndsWith("\"), f, f & "\")
                IO.File.WriteAllText(f & "名言警句语句库.txt", TextBox1.Text, System.Text.Encoding.Default)
            End If
        Catch
        End Try
        Try
            If TextBox1.TextLength <> 0 AndAlso RadioButton2.Checked = True Then
                Form2.Timer1.Enabled = False
                Form2.SL.Clear()
                Form2.SL.AddRange(TextBox1.Text.Split(vbCrLf))
                Form2.Timer1.Enabled = True
            Else
                Form2.Timer1.Enabled = False
                Form2.SL.Clear()
                Form2.SL.AddRange(My.Resources.Strings.Split(vbCrLf))
                Form2.Timer1.Enabled = True
            End If
        Catch

        End Try
        Me.Close()
    End Sub

    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        LoadSetting()
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        Panel1.Enabled = RadioButton2.Checked
    End Sub

    Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
        Me.AcceptButton = Nothing
    End Sub

    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        Me.AcceptButton = Button3
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        If LinkLabel1.Text.StartsWith("[点击") Then
            If MsgBox("为避免误操作,请您确认:" & vbCrLf & "您确定要加入启动项吗?" & vbCrLf & "加入启动项后,当Windows启动后本程序将自动启动!", vbYesNo + vbQuestion, "询问") = DialogResult.Yes Then
                Try
                    Dim KEY As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
                    KEY.SetValue("DS名言警句", Application.ExecutablePath)
                    KEY.Close()
                    RegIsOK.Text = "操作成功"
                    RegIsOK.ForeColor = Color.Green
                    RegIsOK.Show()
                Catch
                    RegIsOK.ForeColor = Color.Red
                    RegIsOK.Text = "操作失败"
                    RegIsOK.Show()
                End Try
            End If
        Else
            If MsgBox("为避免误操作,请您确认:" & vbCrLf & "您确定要从启动项中删除DS名言警句吗?" & vbCrLf & "删除后,当Windows启动时本程序将不会自动启动!", vbYesNo + vbQuestion, "询问") = DialogResult.Yes Then
                Try
                    Dim KEY As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
                    KEY.DeleteValue("DS名言警句", False)
                    KEY.Close()
                    RegIsOK.Text = "操作成功"
                    RegIsOK.ForeColor = Color.Green
                    RegIsOK.Show()
                Catch
                    RegIsOK.ForeColor = Color.Red
                    RegIsOK.Text = "操作失败"
                    RegIsOK.Show()
                End Try
            End If
        End If
        CheckReg()
    End Sub
    Private Sub CheckReg()
        Try
            Dim KEY As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
            Dim S As String = KEY.GetValue("DS名言警句", "")
            If S.Length <> 0 AndAlso S = Application.ExecutablePath Then
                LinkLabel1.Text = "[已经在启动项中,点击此处将DS名言警句从启动项中删除]"
            Else
                LinkLabel1.Text = "[点击此处将DS名言警句放入启动项,以便以后自动启动DS名言警句]"
            End If
            KEY.Close()
        Catch
        End Try
    End Sub
End Class